const { join } = require("path"),
	gulp = require("gulp"),
	gUglify = require("gulp-uglify"),
	rollup = require("rollup").rollup,
	gRename = require("gulp-rename"),
	gClean = require("gulp-clean"),
	commonjs = require("rollup-plugin-commonjs"),
	nodeResolve = require("rollup-plugin-node-resolve"),
	rollbabel = require("rollup-plugin-babel");

gulp.task("clean", () => {
	return gulp.src("./dist/*").pipe(gClean());
});

gulp.task("build", async function buildBaseWC() {
	let bundle = await rollup({
		input: "./index.js",
		plugins: [
			nodeResolve({
				jsnext: true
			}),
			commonjs(),
			rollbabel({
				runtimeHelpers: true
			})
		]
	});
	return bundle.write({
		format: "iife",
		file: join("./dist/", "jsapi.js")
	});
});

gulp.task("mini", () => {
	return gulp
		.src("./dist/jsapi.js")
		.pipe(gUglify({ compress: true }))
		.pipe(
			gRename(path => {
				path.basename += ".min";
				path.extname = ".js";
			})
		)
		.pipe(gulp.dest("./dist"));
});

gulp.task("default", gulp.series("clean", "build", "mini"));
